// findpassword.js
const http = require('../../utils/http.js');
Page({
  data: {
    id_number: '',
    phone: '',
    new_password: ''
  },

  handleInput(e) {
    const { field } = e.currentTarget.dataset;
    this.setData({
      [field]: e.detail.value
    });
  },

  validateInputs() {
    const { id_number, phone, new_password } = this.data;
    // if (!id_number || id_number.length !== 18) {
    //   wx.showToast({ title: '身份证号必须为18位', icon: 'none' });
    //   return false;
    // }
    if (!id_number ) {
      wx.showToast({ title: '身份证号必须为18位', icon: 'none' });
      return false;
    }
    if (!phone) {
      wx.showToast({ title: '电话号不能为空', icon: 'none' });
      return false;
    }
    if (!new_password || new_password.length < 6) {
      wx.showToast({ title: '密码长度必须大于等于6位', icon: 'none' });
      return false;
    }
    return true;
  },

  handleSubmit() {
    if (!this.validateInputs()) return;

    const { id_number, phone, new_password } = this.data;

    const url = `/candidate/modifyPassword?idNumber=${id_number}&phone=${phone}&newPassword=${new_password}`;
    http.post(url).then(res=>{
      console.log(res);
      if(res.data.code == 200){
        wx.showToast({ title: '密码修改成功', icon: 'success' });
        const storedBaseCandidateInfo = wx.getStorageInfoSync("base_candidate_info");
        storedBaseCandidateInfo.password = new_password;
        wx.setStorageSync('base_candidate_info', storedBaseCandidateInfo)
        // 跳转页面
        wx.redirectTo({
          url: `/pages/login/login?id_number= ${id_number}&password=${new_password}`,
        })
      }
    }).catch(err=>{
      console.log(err);
      wx.showToast({ title: '请求失败，请稍后重试', icon: 'none' });
    })
  }
});